package Ships;

import java.io.Serializable;

import logic.Board;

public abstract class Ship implements Serializable{
	
	/**
	 * 
	 */
	
	public final static int HORIZONTAL = 1;
	public final static int VERTICAL = 2;
	
	
	private static final long serialVersionUID = -6715797349550756133L;
	private int size;
	private int life;
	private int orientation; 
	private int lin_ini;
	private int col_ini;
	
	
	public Ship(int size) {
		
		this.size = size;
		this.orientation = HORIZONTAL;
		this.lin_ini = -1;
		this.col_ini = -1;
		this.life = size;
		
	}

	public int getSize() {
		return size;
	}
	
	public int getLife() {
		return life;
	}
	
	public void setLife(int life) {
		this.life = life;
	}
	
	public int getOrientation() {
		return orientation;
	}
	
	public void setOrientation(int orientation) {
		if (orientation==Ship.VERTICAL || orientation==Ship.HORIZONTAL)
			this.orientation = orientation;
	}
	
	public int getCol_ini() {
		return col_ini;
	}
	
	public int getLin_ini() {
		return lin_ini;
	}
	
	public void setLin_ini(int lin_ini) {
		this.lin_ini = lin_ini;
	}
	
	public void setCol_ini(int col_ini) {
		this.col_ini = col_ini;
	}
	
	public boolean isWrecked(){
		return life<1;
	}
	
	/**
	 * Decrements Ship life
	 * 
	 * @return true if decremented, false if cant decrement (Ship is already Wrecked)
	 */
	public boolean hit(){
		if (life>0){
			life--;
			return true;
		}
		else
			return false;
	}
	
	/*
	public boolean verifyLocation(int cols, int lins){

		if(orientation==Ship.HORIZONTAL){

			//vai correr todas as colunas que o barco tem para ver se esta na coluna que se mandou o tiro
			//caso esteja ve logo se esta nessa linha e faz return do true
			
			for(int i=col_ini;i<col_ini + this.getSize();i++)
				if(cols == col_ini + i && lins == this.getLin_ini())
					return true;
		
		}
		else{
			
			for(int i=lin_ini;i<lin_ini + this.getSize();i++)
				if(lins == lin_ini + i && cols == this.getCol_ini())
					return true;

		}

		return false;
	}*/
	
	/**
	 * 
	 * @param board - the Board where the ship is.
	 * @return true if the ship is inside the board limits or false if it isnt.
	 */
	public boolean isAtBoard(Board board){
		
		int lines = board.getNLines();
		int columns = board.getNColumns();
		
		if (col_ini<0 || col_ini>=columns || lin_ini<0 || lin_ini>=lines)
			return false;
		else
			if (orientation==HORIZONTAL && (col_ini + size)>columns)
				return false;
			else
				if (orientation==VERTICAL && (lin_ini + size)>lines)
					return false;
		
		return true;
		
	}

	public void switchOrientation() {
	
		if (orientation==HORIZONTAL)
			orientation=VERTICAL;
		else
			orientation = HORIZONTAL;
		
	}

	
	/**
	 * 
	 * @param board - The Board where the ship is.
	 * @return TRUE if the Ship is inside the board and in a position 
	 * where there's no other ship or FALSE if not.
	 */
	public boolean isWellPositioned(Board board) {
		
		if (!isAtBoard(board))
			return false;
		
		
		
		if (orientation==HORIZONTAL){
			
			for (int col=col_ini; col < (col_ini+size); col++){

				if (board.getNumShipsAt(lin_ini, col)!=1)
					return false;
			}
		}
		else
			if (orientation==VERTICAL){
				
				for (int line=lin_ini; line < (lin_ini+size); line++){
					
					if (board.getNumShipsAt(line, col_ini)!=1)
						return false;
				}
				
			}
			else
				return false; //unknown orientation
		
		return true;
	}

}